﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using BitPortal.Utils;
namespace BitPortal.Modules
{
    public partial class FileSelecter : System.Web.UI.UserControl
    {
        public string FieldLabel { set; get; }
        public string FileType { set; get; }
        public string StartFolder { set; get; }
        public Ext.Net.TextField TextFieldURL
        {
            get { return this.TextField1; }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (FileType.Equals("Image"))
            {
                this.TextField1.Listeners.Blur.Handler = "LoadImage('" + imagepreview.ClientID + "',this.getValue(),'" + button_download.ClientID + "');";
                this.TextField1.Listeners.Change.Handler = "LoadImage('" + imagepreview.ClientID + "',this.getValue(),'" + button_download.ClientID + "');";
            }
        }
        protected override void OnPreRender(EventArgs e)
        {
            if (string.IsNullOrEmpty(FileType)) { FileType = "Image"; }
            if (string.IsNullOrEmpty(StartFolder)) { StartFolder = "~/Upload"; }
            TextField1.FieldLabel = string.IsNullOrEmpty(this.FieldLabel) ? "Đường dẫn File" : this.FieldLabel;
            base.OnPreRender(e);
        }

        protected void btnDownload_Click(object sender, DirectEventArgs e)
        {
            TextField1.Text = BitPortal.Utils.ImageUtils.SaveImage(TextField1.Text);
        }

        protected void btnUpload_Click(object sender, DirectEventArgs e)
        {
            try
            {
                TextField1.Text = FileUploadField1.PostedFile.UploadFile(targetFolder: StartFolder, allowType: this.FileType);
                wUpload.Hide();
            }
            catch (Exception ex)
            {
                X.Msg.Alert("Thông báo lỗi", ex.Message).Show();
            }
        }
    }
}